const fs = require('fs');

const pump = require('pump');

const compressing = require('compressing');

const  SUFFIX_NAME = '.tar'
// const tarStream = new compressing.tar.Stream();

// 这个tar方法建议不用
// function tar(files,dest){
//   dest = dest+'.tar';
//   for(let item of files){
//     tarStream.addEntry(item);
//   }
//   const destStream = fs.createWriteStream(dest);
//   pump(tarStream, destStream, handleError);
//   function handleError(err){
//     if(err){
//       console.log(err);
//       fs.unlinkSync(dest);
//       process.exit();
//     }else {
//       gzip(dest);
//     }
//   }
// }
function gzip(dest,callback){

  const source = fs.createReadStream(dest);

  const target = fs.createWriteStream(dest + SUFFIX_NAME);

  pump(source, new compressing.gzip.FileStream(), target, err => {

    if (err) {

      console.error(err);

    } else {

      console.log('success');

      fs.unlinkSync(dest);

      if(callback instanceof Function)callback(dest + SUFFIX_NAME);

    }
    
  });
}
module.exports={
  gzip
}